package com.suke.czx.modules.performance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.ExcelUtil;
import com.suke.czx.common.utils.ExportTypeEnum;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.performance.entity.Performance;
import com.suke.czx.modules.performance.entity.PerformanceSearchBo;
import com.suke.czx.modules.performance.mapper.PerformanceMapper;
import com.suke.czx.modules.performance.service.PerformanceService;
import com.suke.czx.modules.performance.template.PerformanceTemplate;
import lombok.AllArgsConstructor;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 绩效
 * 代码描述: 绩效业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/27 2:36
 */
@Service
@AllArgsConstructor
public class PerformanceServiceImpl extends ServiceImpl<PerformanceMapper, Performance> implements PerformanceService {

    private final PerformanceMapper performanceMapper;

    private final PerformanceTemplate performanceTemplate;

    /**
     * 分页查询绩效列表
     * @param performanceSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pagePerformanceList(PerformanceSearchBo performanceSearchBo) {
        Page<Performance> page = PageHelper.startPage(performanceSearchBo.getPage(), performanceSearchBo.getLimit(), true,true,true);
        // 获取绩效查询结果集
        List<Performance> performanceList = performanceMapper.selectPerformanceList(performanceSearchBo);
        // 加和该查询条件下所有绩效分数和
        Double sumPerformanceCategory = performanceMapper.sumPerformanceCategory(performanceSearchBo);
        // 返回结果
        return Objects.requireNonNull(Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal()))
                .put(FieldConstant.PERFORMANCE_CATEGORY, sumPerformanceCategory)).setData(Optional.ofNullable(performanceList).orElse(Lists.newArrayList()));
    }

    /**
     * 删除绩效
     * @param performanceId 绩效编码
     * @param userId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelPerformance(Long performanceId, Long userId) {
        performanceMapper.updateDelPerformance(performanceId, userId);
        return R.ok();
    }

    /**
     * 导出绩效
     * @param performanceSearchBo 查询BO
     * @param response 响应
     */
    @Override
    public void exportPerformance(PerformanceSearchBo performanceSearchBo, HttpServletResponse response) {
        // 查询绩效列表
        List<Performance> dataList = performanceMapper.selectPerformanceList(performanceSearchBo);
        // 执行导出获取SXSSFWorkbook
        SXSSFWorkbook workbook = performanceTemplate.createPerformanceExcel(dataList);
        // 执行写文件
        ExcelUtil.writeExcel(response, workbook, ExportTypeEnum.PERFORMANCE.getStatement());
    }

}
